#!/usr/bin/python
#coding=utf-8

from jinja2 import Environment, FileSystemLoader
from config.cnf import WEB_CONFIG
import os
from web.session import Session

def render(template_name, args=None):
    env = Environment(
            loader = FileSystemLoader(os.path.join(WEB_CONFIG['ROOTPATH'], WEB_CONFIG['TEMPLATES'])),
            auto_reload = True
    )   
    env.autoescape = True
    template = env.get_template(template_name)
    if args:
        page = template.render(args).encode('utf8')
    else:
        page = template.render().encode('utf8')
    return page

def set_navbar_token(req, set_token=False):
    session = Session(req)
    username = session.get('username')
    token = session.get('token')
    logout, navbar = u'登出', {}
    navbar['username'] = username
    navbar['logout'] = logout
    if set_token:
        navbar['token'] = token
    return navbar


    





